//
//  WelcomeViewController.swift
//  Weibo
//
//  Created by 周先生 on 15/11/12.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class WelcomeViewController: UIViewController {

    override func loadView() {
        view = backImage
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    //注意:不要再viewDidLoad中调用动画的方法  会造成紊乱
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //调用动画的方法
        startAnimation()
    }
    //添加动画
    private func startAnimation() {
    
        /**
        一般在自动布局中使用动画效果 不建议直接修改frame
        自动布局系统 会根据设置的约束 在layoutSubViews方法中自动修改frame
        frame是由自动布局来控制的
        如果希望提前修改约束效果需要调用layoutIfNeeded()来让约束提前刷新 更新frame
        
        */
        //外部改变约束
        self.iconImage.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        
        //动画效果 由近到远 涉及到加速度
        //usingSpringWithDamping:弹簧系数0-1越小越弹
        //initialSpringVelocity:加速度
        //弹簧效果 * 10 = 加速度
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //闭包内部强制刷新界面
            //将提前收集的约束变化进行更新 
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //发出通知
                     NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewController, object: nil)
                })
                
        }
    
    }
    //添加子视图
    private func setupUI() {
    //添加子视图
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        
        //设置布局
        //头像
        iconImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.height.equalTo(100)
            make.width.equalTo(100)
        }
        
        //文字
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImage.snp_bottom).offset(15)
            
        }
        //加载图片
        iconImage.sd_setImageWithURL(UserAccountViewModel().headUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        //给图片处理成圆角
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true  //为什么不加这句话也行
    }
    //懒加载所有的视图控件
    //背景视图
    private lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var iconImage: UIImageView = UIImageView()
    private lazy var welcomeLabel: UILabel = {
        let l = UILabel()
        l.textColor = UIColor.blueColor()
        l.text = (UserAccountViewModel().userName ?? "") + "微博的冬天来了"

//        print(UserAccountViewModel().userName)
            l.font = UIFont.systemFontOfSize(22)
        l.textAlignment = .Center
        l.alpha = 0
        l.sizeToFit()
        return l
    }()
    
}
